#

| Chapter 23 Removing Files |

## 23.12 Using Wildcards to Delete Files with Strange Names

Filenames can be hard to handle if their names include control characters or characters that are special to the shell. Here's a directory with three oddball filenames:

>     % ls
>     What now
>     a$file
>     prog|.c
>     program.c

When you type those filenames on the command line, the shell interprets the special characters (space, dollar sign, and vertical bar) instead of including them as part of the filename. There are several ways (23.11) to handle this problem. One is with wildcards (15.2). Type a part of the filename without the weird characters and use a wildcard to match the rest. As article 8.5 explains, the shell doesn't scan the filenames for other special characters after it interprets the wildcards, so you're (usually) safe if you can get a wildcard to match. For example, here's how to rename What now to Whatnow, remove a$file, and rename prog|.c to prog.c:

>     % mv What* Whatnow
>     % rm -i a*
>     rm: remove a$file? y
>     % mv prog?.c prog.c

Filenames with control characters are just another version of the same problem. Use a wildcard to match the part of the name that's troubling you. The real problem with control characters with filenames is that some control characters do weird things to your screen. Once I accidentally got a file with a CTRL-L in its name. Whenever I ran ls, it erased the screen before I could see what the filename was! Article 16.14 explains that on a BSD-based UNIX system, you can use ls -q instead of a plain ls; on System V, use ls -b. It should be easy to spot the offensive file and construct a wildcard expression to rename or delete it. (ls -q is the default on many modern BSD UNIX implementations. So if you're a BSD user, you may never see this problem.)

- JP

23.11 Deleting Files with Odd Names |  | 23.13 Deleting Files with the Null Name